import { defineStore } from 'pinia'
import * as userApi from '@/api/user'

export const useUserStore = defineStore('user', {
  state: () => ({
    // 用户列表
    userList: [],
    // 总记录数
    total: 0,
    // 加载状态
    loading: false,
    // 错误信息
    error: null
  }),

  getters: {
    // 获取用户列表
    getUsers: (state) => state.userList,
    // 获取总数
    getTotal: (state) => state.total,
    // 获取加载状态
    isLoading: (state) => state.loading
  },

  actions: {
    /**
     * 加载用户列表
     * @param {Object} params 查询参数
     */
    async loadUserList(params = {}) {
      this.loading = true
      this.error = null
      try {
        const response = await userApi.getUserList(params)
        if (response.code === 1) {
          this.userList = response.data.rows || []
          this.total = response.data.total || 0
        }
      } catch (error) {
        this.error = error.message
      } finally {
        this.loading = false
      }
    },

    /**
     * 获取所有用户
     */
    async loadAllUsers() {
      this.loading = true
      this.error = null
      try {
        const response = await userApi.getAllUsers()
        if (response.code === 1) {
          this.userList = response.data || []
        }
      } catch (error) {
        this.error = error.message
      } finally {
        this.loading = false
      }
    },

    /**
     * 添加用户
     * @param {Object} userData 用户数据
     * @returns {Promise} 添加结果
     */
    async addUser(userData) {
      this.loading = true
      this.error = null
      try {
        const response = await userApi.addUser(userData)
        if (response.code === 1) {
          // 添加成功后重新加载列表
          await this.loadUserList()
          return { success: true }
        }
        return { success: false, message: response.msg }
      } catch (error) {
        this.error = error.message
        return { success: false, message: error.message }
      } finally {
        this.loading = false
      }
    },

    /**
     * 更新用户
     * @param {Object} userData 用户数据
     * @returns {Promise} 更新结果
     */
    async updateUser(userData) {
      this.loading = true
      this.error = null
      try {
        const response = await userApi.updateUser(userData)
        if (response.code === 1) {
          // 更新成功后重新加载列表
          await this.loadUserList()
          return { success: true }
        }
        return { success: false, message: response.msg }
      } catch (error) {
        this.error = error.message
        return { success: false, message: error.message }
      } finally {
        this.loading = false
      }
    },

    /**
     * 删除用户
     * @param {string} ids 用户ID，多个用逗号分隔
     * @returns {Promise} 删除结果
     */
    async deleteUser(ids) {
      this.loading = true
      this.error = null
      try {
        const response = await userApi.deleteUser(ids)
        if (response.code === 1) {
          // 删除成功后重新加载列表
          await this.loadUserList()
          return { success: true }
        }
        return { success: false, message: response.msg }
      } catch (error) {
        this.error = error.message
        return { success: false, message: error.message }
      } finally {
        this.loading = false
      }
    },

    /**
     * 重置状态
     */
    resetState() {
      this.userList = []
      this.total = 0
      this.loading = false
      this.error = null
    }
  }
})